Advertisement
Guest User

Untitled

a guest
Sep 20th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.20 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Windows.Forms;
  5. using System.Collections.Specialized;
  6. using System.Xml;
  7. using System.IO;
  8.  
  9. namespace BiblioUpTik
  10. {
  11.     public class PortableSettingsProvider : SettingsProvider
  12.     {
  13.         const string SETTINGSROOT = "Settings";
  14.         //XML Root Node
  15.  
  16.         public override void Initialize(string name, NameValueCollection col)
  17.         {
  18.             base.Initialize(this.ApplicationName, col);
  19.         }
  20.  
  21.         public override string ApplicationName
  22.         {
  23.             get
  24.             {
  25.                 if (Application.ProductName.Trim().Length > 0)
  26.                 {
  27.                     return Application.ProductName;
  28.                 }
  29.                 else
  30.                 {
  31.                     FileInfo fi = new FileInfo(Application.ExecutablePath);
  32.                     return fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length);
  33.                 }
  34.             }
  35.             set { }
  36.             //Do nothing
  37.         }
  38.  
  39.         public override string Name
  40.         {
  41.             get { return "PortableSettingsProvider"; }
  42.         }
  43.         public virtual string GetAppSettingsPath()
  44.         {
  45.             //Used to determine where to store the settings
  46.             System.IO.FileInfo fi = new System.IO.FileInfo(Application.ExecutablePath);
  47.             return fi.DirectoryName;
  48.         }
  49.  
  50.         public virtual string GetAppSettingsFilename()
  51.         {
  52.             //Used to determine the filename to store the settings
  53.             return ApplicationName + ".settings";
  54.         }
  55.  
  56.         public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection propvals)
  57.         {
  58.             //Iterate through the settings to be stored
  59.             //Only dirty settings are included in propvals, and only ones relevant to this provider
  60.             foreach (SettingsPropertyValue propval in propvals)
  61.             {
  62.                 SetValue(propval);
  63.             }
  64.  
  65.             try
  66.             {
  67.                 SettingsXML.Save(Path.Combine(GetAppSettingsPath(), GetAppSettingsFilename()));
  68.             }
  69.             catch (Exception ex)
  70.             {
  71.             }
  72.             //Ignore if cant save, device been ejected
  73.         }
  74.  
  75.         public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection props)
  76.         {
  77.             //Create new collection of values
  78.             SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
  79.  
  80.             //Iterate through the settings to be retrieved
  81.             foreach (SettingsProperty setting in props)
  82.             {
  83.  
  84.                 SettingsPropertyValue value = new SettingsPropertyValue(setting);
  85.                 value.IsDirty = false;
  86.                 value.SerializedValue = GetValue(setting);
  87.                 values.Add(value);
  88.             }
  89.             return values;
  90.         }
  91.  
  92.         private XmlDocument _settingsXML = null;
  93.  
  94.         private XmlDocument SettingsXML
  95.         {
  96.             get
  97.             {
  98.                 //If we dont hold an xml document, try opening one.  
  99.                 //If it doesnt exist then create a new one ready.
  100.                 if (_settingsXML == null)
  101.                 {
  102.                     _settingsXML = new XmlDocument();
  103.  
  104.                     try
  105.                     {
  106.                         _settingsXML.Load(Path.Combine(GetAppSettingsPath(), GetAppSettingsFilename()));
  107.                     }
  108.                     catch (Exception ex)
  109.                     {
  110.                         //Create new document
  111.                         XmlDeclaration dec = _settingsXML.CreateXmlDeclaration("1.0", "utf-8", string.Empty);
  112.                         _settingsXML.AppendChild(dec);
  113.  
  114.                         XmlNode nodeRoot = default(XmlNode);
  115.  
  116.                         nodeRoot = _settingsXML.CreateNode(XmlNodeType.Element, SETTINGSROOT, "");
  117.                         _settingsXML.AppendChild(nodeRoot);
  118.                     }
  119.                 }
  120.  
  121.                 return _settingsXML;
  122.             }
  123.         }
  124.  
  125.         private string GetValue(SettingsProperty setting)
  126.         {
  127.             string ret = "";
  128.  
  129.             try
  130.             {
  131.                 if (IsRoaming(setting))
  132.                 {
  133.                     ret = SettingsXML.SelectSingleNode(SETTINGSROOT + "/" + setting.Name).InnerText;
  134.                 }
  135.                 else
  136.                 {
  137.                     ret = SettingsXML.SelectSingleNode(SETTINGSROOT + "/" + Environment.MachineName + "/" + setting.Name).InnerText;
  138.                 }
  139.             }
  140.  
  141.             catch (Exception ex)
  142.             {
  143.                 if ((setting.DefaultValue != null))
  144.                 {
  145.                     ret = setting.DefaultValue.ToString();
  146.                 }
  147.                 else
  148.                 {
  149.                     ret = "";
  150.                 }
  151.             }
  152.  
  153.             return ret;
  154.         }
  155.  
  156.         private void SetValue(SettingsPropertyValue propVal)
  157.         {
  158.  
  159.             XmlElement MachineNode = default(XmlElement);
  160.             XmlElement SettingNode = default(XmlElement);
  161.  
  162.             //Determine if the setting is roaming.
  163.             //If roaming then the value is stored as an element under the root
  164.             //Otherwise it is stored under a machine name node
  165.             try
  166.             {
  167.                 if (IsRoaming(propVal.Property))
  168.                 {
  169.                     SettingNode = (XmlElement)SettingsXML.SelectSingleNode(SETTINGSROOT + "/" + propVal.Name);
  170.                 }
  171.                 else
  172.                 {
  173.                     SettingNode = (XmlElement)SettingsXML.SelectSingleNode(SETTINGSROOT + "/" + Environment.MachineName + "/" + propVal.Name);
  174.                 }
  175.             }
  176.             catch (Exception ex)
  177.             {
  178.                 SettingNode = null;
  179.             }
  180.  
  181.             //Check to see if the node exists, if so then set its new value
  182.             if ((SettingNode != null))
  183.             {
  184.                 SettingNode.InnerText = propVal.SerializedValue.ToString();
  185.             }
  186.             else
  187.             {
  188.                 if (IsRoaming(propVal.Property))
  189.                 {
  190.                     //Store the value as an element of the Settings Root Node
  191.                     SettingNode = SettingsXML.CreateElement(propVal.Name);
  192.                     SettingNode.InnerText = propVal.SerializedValue.ToString();
  193.                     SettingsXML.SelectSingleNode(SETTINGSROOT).AppendChild(SettingNode);
  194.                 }
  195.                 else
  196.                 {
  197.                     //Its machine specific, store as an element of the machine name node,
  198.                     //creating a new machine name node if one doesnt exist.
  199.                     try
  200.                     {
  201.  
  202.                         MachineNode = (XmlElement)SettingsXML.SelectSingleNode(SETTINGSROOT + "/" + Environment.MachineName);
  203.                     }
  204.                     catch (Exception ex)
  205.                     {
  206.                         MachineNode = SettingsXML.CreateElement(Environment.MachineName);
  207.                         SettingsXML.SelectSingleNode(SETTINGSROOT).AppendChild(MachineNode);
  208.                     }
  209.  
  210.                     if (MachineNode == null)
  211.                     {
  212.                         MachineNode = SettingsXML.CreateElement(Environment.MachineName);
  213.                         SettingsXML.SelectSingleNode(SETTINGSROOT).AppendChild(MachineNode);
  214.                     }
  215.  
  216.                     SettingNode = SettingsXML.CreateElement(propVal.Name);
  217.                     SettingNode.InnerText = propVal.SerializedValue.ToString();
  218.                     MachineNode.AppendChild(SettingNode);
  219.                 }
  220.             }
  221.         }
  222.  
  223.         private bool IsRoaming(SettingsProperty prop)
  224.         {
  225.             //Determine if the setting is marked as Roaming
  226.             foreach (DictionaryEntry d in prop.Attributes)
  227.             {
  228.                 Attribute a = (Attribute)d.Value;
  229.                 if (a is System.Configuration.SettingsManageabilityAttribute)
  230.                 {
  231.                     return true;
  232.                 }
  233.             }
  234.             return false;
  235.         }
  236.     }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement